#include "oled.h"
#include "stdlib.h"
#include "oledfont.h"

#include "i2c.h"

u8 OLED_GRAM[2][128][8];

volatile uint8_t dma_finish = 1;
volatile uint8_t dma_num = 0;

volatile uint8_t oled_gram_page = 0;

//����һ���ֽ�
//mode:����/�����־ 0,��ʾ����;1,��ʾ����;
void OLED_WR_Byte(u8 dat,u8 mode)
{
//	I2C_Start();
//	Send_Byte(0x78); // 0111 1000
//	I2C_WaitAck();
//	if(mode){Send_Byte(0x40);}
//  else{Send_Byte(0x00);}
//	I2C_WaitAck();
//	Send_Byte(dat);
//	I2C_WaitAck();
//	I2C_Stop();
	
//	if(mode) HAL_I2C_Mem_Write(&hi2c1, 0x78, 0x40, I2C_MEMADD_SIZE_8BIT, &dat, 1, 0xf);
//	else HAL_I2C_Mem_Write(&hi2c1, 0x78, 0x00, I2C_MEMADD_SIZE_8BIT, &dat, 1, 0xf);
	
	if(dma_finish == 1)
	{
		dma_finish = 0;
		if(mode) HAL_I2C_Mem_Write_DMA(&hi2c1, 0x78, 0x40, I2C_MEMADD_SIZE_8BIT, &dat, 1);
		else HAL_I2C_Mem_Write_DMA(&hi2c1, 0x78, 0x00, I2C_MEMADD_SIZE_8BIT, &dat, 1);
	}
	else
	{
		while(dma_finish == 0);
		dma_finish = 0;
		if(mode) HAL_I2C_Mem_Write_DMA(&hi2c1, 0x78, 0x40, I2C_MEMADD_SIZE_8BIT, &dat, 1);
		else HAL_I2C_Mem_Write_DMA(&hi2c1, 0x78, 0x00, I2C_MEMADD_SIZE_8BIT, &dat, 1);
	}

}

//���Ժ���
void OLED_ColorTurn(u8 i)
{
	if(i==0)
		{
			OLED_WR_Byte(0xA6,OLED_CMD);//������ʾ
		}
	if(i==1)
		{
			OLED_WR_Byte(0xA7,OLED_CMD);//��ɫ��ʾ
		}
}

//����OLED��ʾ 
void OLED_DisPlay_On(void)
{
	OLED_WR_Byte(0x8D,OLED_CMD);//��ɱ�ʹ��
	OLED_WR_Byte(0x14,OLED_CMD);//������ɱ�
	OLED_WR_Byte(0xAF,OLED_CMD);//������Ļ
}

//�ر�OLED��ʾ 
void OLED_DisPlay_Off(void)
{
	OLED_WR_Byte(0x8D,OLED_CMD);//��ɱ�ʹ��
	OLED_WR_Byte(0x10,OLED_CMD);//�رյ�ɱ�
	OLED_WR_Byte(0xAE,OLED_CMD);//�ر���Ļ
}

void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c)
{
	dma_finish = 1;
	dma_num += 1;
}

//�����Դ浽OLED	
void OLED_Refresh(void)
{	
	if(dma_finish == 1)
	{
		dma_finish = 0;
		HAL_I2C_Mem_Write_DMA(&hi2c1, 0x78, 0x40, I2C_MEMADD_SIZE_8BIT, OLED_GRAM[oled_gram_page][0], 128 * 8);
		memcpy(OLED_GRAM[1 - oled_gram_page], OLED_GRAM[oled_gram_page], 128 * 8);
		oled_gram_page = 1 - oled_gram_page;
	}
}

//��������
void OLED_Clear(void)
{
	u8 i,n;
	for(i=0;i<8;i++)
	{
	   for(n=0;n<128;n++)
			{
			 OLED_GRAM[oled_gram_page][n][i]=0;//�����������
			}
  }
	OLED_Refresh();//������ʾ
}

void OLED_Init(void)
{
//	GPIO_InitTypeDef  GPIO_InitStructure;
// 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);	 //ʹ��A�˿�ʱ��
//	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;	 
// 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 //�������
//	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//�ٶ�50MHz
// 	GPIO_Init(GPIOB, &GPIO_InitStructure);	  //��ʼ��PA0,1
// 	GPIO_SetBits(GPIOB,GPIO_Pin_8|GPIO_Pin_9);
	
	OLED_RES_Clr();
	HAL_Delay(200);
	OLED_RES_Set();
	
	OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel
	
	OLED_WR_Byte(0x00,OLED_CMD);    /*set lower column address*/       
	OLED_WR_Byte(0x10,OLED_CMD);    /*set higher column address*/

	OLED_WR_Byte(0xB0,OLED_CMD);    /*set page address*/

	OLED_WR_Byte(0x81,OLED_CMD);    /*contract control*/
	OLED_WR_Byte(0x5f,OLED_CMD);    /*128*/
	
	if(USE_HORIZONTAL==0)
	{
		OLED_WR_Byte(0x20,OLED_CMD);    /* Set Memory addressing mode (0x20/0x21) */
		OLED_WR_Byte(0x09,OLED_CMD);     /* 0x09 */

		OLED_WR_Byte(0xA1,OLED_CMD);    /*set segment remap  0XA1 */
		OLED_WR_Byte(0xC8,OLED_CMD);    /*Com scan direction   0Xc8  */
	}
	else if(USE_HORIZONTAL==90)
	{
		OLED_WR_Byte(0x20,OLED_CMD);    /* Set Memory addressing mode (0x20/0x21) */
		OLED_WR_Byte(0x01,OLED_CMD);     /* 0x02 */

		OLED_WR_Byte(0xA1,OLED_CMD);    /*set segment remap  0XA1 */
		OLED_WR_Byte(0xC0,OLED_CMD);    /*Com scan direction   0Xc0  */
	}
	else if(USE_HORIZONTAL==180)
	{
		OLED_WR_Byte(0x20,OLED_CMD);    /* Set Memory addressing mode (0x20/0x21) */
		OLED_WR_Byte(0x09,OLED_CMD);     /* 0x09 */

		OLED_WR_Byte(0xA0,OLED_CMD);    /*set segment remap  0XA0 */
		OLED_WR_Byte(0xC0,OLED_CMD);    /*Com scan direction   0Xc0  */
	}
	else if(USE_HORIZONTAL==270)
	{
		OLED_WR_Byte(0x20,OLED_CMD);    /* Set Memory addressing mode (0x20/0x21) */
		OLED_WR_Byte(0x09,OLED_CMD);     /* 0x02 */

		OLED_WR_Byte(0xA0,OLED_CMD);    /*set segment remap  0XA0 */
		OLED_WR_Byte(0xC8,OLED_CMD);    /*Com scan direction   0Xc8  */
	}
	
	OLED_WR_Byte(0xA4,OLED_CMD);    /*Disable Entire Display On (0xA4/0xA5)*/ 

	OLED_WR_Byte(0xA6,OLED_CMD);    /*normal / reverse*/

	OLED_WR_Byte(0xA8,OLED_CMD);    /*multiplex ratio*/
	OLED_WR_Byte(0x3F,OLED_CMD);    /*duty = 1/64*/

	OLED_WR_Byte(0xD3,OLED_CMD);    /*set display offset*/
	OLED_WR_Byte(0x00,OLED_CMD);    /*   0x20   */

	OLED_WR_Byte(0xD5,OLED_CMD);    /*set osc division*/
	OLED_WR_Byte(0x80,OLED_CMD);    

	OLED_WR_Byte(0xD9,OLED_CMD);    /*set pre-charge period*/
	OLED_WR_Byte(0x22,OLED_CMD);

	OLED_WR_Byte(0xDA,OLED_CMD);    /* Set SEG Pins Hardware Configuration */
	OLED_WR_Byte(0x10,OLED_CMD);

	OLED_WR_Byte(0xdb,OLED_CMD);    /*set vcomh*/
	OLED_WR_Byte(0x30,OLED_CMD);

	OLED_WR_Byte(0x8d,OLED_CMD);    /*set charge pump enable*/
	OLED_WR_Byte(0x72,OLED_CMD);    /* 0x12:7.5V; 0x52:8V;  0x72:9V;  0x92:10V */
	OLED_Clear();
	OLED_WR_Byte(0xAF,OLED_CMD);
}

